/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.unibo.as.HeartsLogic;

/**
 *
 * @author lilith
 */
public class GameLogic 
{
    private int gameState;
    private final boolean bChangeCard;
    private int playerTurn;
    private Player playerArray[];
    
    private int nPlayerReady;

    public GameLogic(int nPlayerBots, int nTurn) 
    {
        nPlayerReady = 0;
        gameState = Common.TURN_INIT;
        if(nTurn > 1)
        {
            bChangeCard = true;
        }
        else
        {
            bChangeCard = false;
        }
        
        playerArray = new Player[Common.PLAYER_NUM];
        
        
        for(int i = 0; i < nPlayerBots; i++)
        {
            PlayerBots pb = new PlayerBots(i);
            addNewPlayer(pb);
        }
        
        playerTurn = -1;
    }

    
    public boolean addPlayer(Player p)
    {
        return addNewPlayer(p);
    }
    
    private boolean addNewPlayer(Player p) 
    {
        boolean bRet = false;
        boolean bPlayerExist = false;
        
        for(int i = 0; (i < Common.PLAYER_NUM) && !bPlayerExist; i++)
        {
            if(playerArray[i] != null)
            {
                String strID = playerArray[i].getPlayerID();
                if(strID.compareToIgnoreCase(p.getPlayerID()) == 0)
                {
                    bPlayerExist = true;
                }
            }
        }
        
        if(bPlayerExist)
        {
            return bRet;
        }
        
        if(gameState == Common.TURN_INIT)
        {
            int nIndex = (int)(Math.random() * Common.PLAYER_NUM);

            boolean bContinue = true;
            for(int i = 0; (i < Common.PLAYER_NUM) && bContinue; i++)
            {
                if(playerArray[nIndex] == null)
                {
                    bContinue = false;
                }
                else
                {
                    nIndex = (nIndex + 1)%Common.PLAYER_NUM;
                }
            }

            if(!bContinue)
            {
                playerArray[nIndex] = p;
                nPlayerReady++;
                bRet = true;
            }
        }

        return bRet;
    }
    
    public boolean removePlayer(String strPlayerID)
    {
        boolean bRet = false;
        boolean bPlayerExist = false;
        int nIndex = -1;
        
        for(int i = 0; (i < Common.PLAYER_NUM) && !bPlayerExist; i++)
        {
            if(playerArray[i] != null)
            {
                String strID = playerArray[i].getPlayerID();
                if(strID.compareToIgnoreCase(strPlayerID) == 0)
                {
                    bPlayerExist = true;
                    nIndex = i;
                }
            }
        }
        
        if(!bPlayerExist)
        {
            return bRet;
        }
        
        if(gameState == Common.TURN_INIT)
        {
            Player p = playerArray[nIndex];
            playerArray[nIndex] = null;
            nPlayerReady--;
            bRet = true;
        }
        else
        {
            //creare giocatore Bots
        }
        
        return bRet;
    }

    void gameDelete() {
        gameState = Common.TURN_TERMINATE;
        for(int i = 0; i < Common.PLAYER_NUM; i++)
        {
            playerArray[i] = null;
        }
    }

    public GameState getGameState(String strPlayerID)
    {        
        boolean bActivePlayer = false;
        int nPlayer = 0;
        boolean bContinue = true;
        for(int i = 0; (i < Common.CARD_NUM) && bContinue; i++)
        {
            if(playerArray[i].strPlayerID.compareToIgnoreCase(strPlayerID) == 0)
            {
                bActivePlayer = true;
                nPlayer = i;
                bContinue = false;
            }
        }
            
        GameState gs = new GameState(gameState, bActivePlayer, nPlayer, playerArray);
        return gs;
    }
    
    boolean nextTurn()
    {
        boolean bChange = false;
        switch(gameState)
        {
            case Common.TURN_INIT:
                if(nPlayerReady == Common.PLAYER_NUM)
                {
                    DeckCard dc = new DeckCard();
                    dc.getPlayerCard(playerArray);
                    gameState = Common.TURN_DEALDECK;
                    bChange = true;
                }
                break;
            case Common.TURN_DEALDECK:
                break;
            case Common.TURN_CANGECARDS:
                break;
            case Common.TURN_PLAYCARD:
                break;
            case Common.TURN_EVALTURN:
                break;
            case Common.TURN_ENDTURN:
                break;
            case Common.TURN_ENDGAME:
                break;
            case Common.TURN_TERMINATE:
                break;
            default:
                break;
        }
        
        return bChange;
    }
    
    public boolean playCard(String strPlayerID, Card card)
    {
        return true;
    }

    public Player[] getPlayerArray() {
        return playerArray;
    }

    public void setPlayerArray(Player[] playerArray) {
        this.playerArray = playerArray;
    }

    
    //changeCard    
}

